var levelOrder = function (root) {
  if (!root) return [];
  let res = [];
      queue = [root];
  while (queue.length) {
    let len = queue.length;
    let curLevel = [];
    while (len--) {
      let cur = queue.shift();
      curLevel.push(cur.val);
      if (cur.left) queue.push(cur.left);
      if (cur.right) queue.push(cur.right);
    }
    res.push(curLevel);
  }
  return res;
}